/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.controller
 * @className: WxGoodsController
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/31 22:19
 * @version: 1.8
 */

package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.bo.WxGoodsSearcherBo;
import com.cskaoyan.bean.common.BaseRespVo;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCategoryDataVo;
import com.cskaoyan.bean.vo.WxGoodListVo;
import com.cskaoyan.bean.vo.WxGoodsDetailVo;
import com.cskaoyan.bean.vo.WxSearchGoodsVo;
import com.cskaoyan.service.WxGoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;


@RequestMapping("wx/goods")
@RestController
public class WxGoodsController {
    @Resource
    WxGoodsService goodsService;

    @GetMapping("list")
    public BaseRespVo getGoodsInfoBySearch(MarketGoods goodsSearcherBo) {
        System.out.println(goodsSearcherBo);
        WxSearchGoodsVo wxSearchGoodsVo = goodsService.getGoodsInfoBySearch(goodsSearcherBo);
        System.out.println(goodsSearcherBo);
        return BaseRespVo.ok(wxSearchGoodsVo);
    }

    /**
     * 类别功能
     *
     * @param id
     * @return
     */
    @RequestMapping("category")
    public BaseRespVo category(Integer id) {
        WxCategoryDataVo data = goodsService.category(id);
        return BaseRespVo.ok(data, "成功");
    }

    /**
     * 关联商品展示
     *
     * @param id
     * @return
     */
    @RequestMapping("related")
    public BaseRespVo related(Integer id) {
        //根据id查询关联商品
        CommonData<MarketGoods> related = goodsService.related(id);
        //封装返回值
        List<MarketGoods> relatedList = related.getList();
        List<WxGoodListVo> list = new ArrayList<>();
        for (MarketGoods marketGoods : relatedList) {
            if (!ObjectUtils.isEmpty(marketGoods)) {
                WxGoodListVo goodListVo = new WxGoodListVo();
                BeanUtils.copyProperties(marketGoods, goodListVo);
                list.add(goodListVo);
            }
        }
        CommonData<WxGoodListVo> data = new CommonData<>();
        BeanUtils.copyProperties(related, data);
        data.setList(list);
        return BaseRespVo.ok(data, "成功");
    }


    /**
     * 小程序下方分类板块刚进入时候的上方搜索栏，货物总数信息显示
     * 小程序下方分类板块刚进入时候的上方搜索栏，货物总数信息显示
     * wx/goods/count接口的实现
     *
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author landz
     * @create 2023/1/2 16:28
     */
    @GetMapping("count")
    public BaseRespVo count() {
        Integer wxGoodsCountVo = goodsService.getGoodsCount();
        return BaseRespVo.ok(wxGoodsCountVo);
    }
    @GetMapping("detail")
    public BaseRespVo getGoodsDetail(Integer id){
        WxGoodsDetailVo wxGoodsDetailVo=goodsService.getGoodsDetailInfo(id);
        return BaseRespVo.ok(wxGoodsDetailVo,"成功");
    }

}



